package com.fau.hm.action.index;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.hm.action.PublicAction;
import com.fau.hm.entity.MedicalAdmin;
import com.fau.hm.entity.MedicalGroup;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 组信息维护Action
 * @author dangjh
 * @datetime 2014年10月14日,下午10:54:09
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class GroupAction extends PublicAction implements ModelDriven<MedicalGroup> {

	private static final long serialVersionUID = 32007810302187786L;
	
	private MedicalGroup group = new MedicalGroup();
	
	/**
	 * @desc 实例化组列表页
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月12日,下午6:20:00
	 */
	@Action(value = "init_group_list", results = {@Result(name = SUCCESS, location = "init_group_list.jsp")})
	public String initGroupList(){
		return SUCCESS;
	}

	/**
	 * @desc 组数据列表展示
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:39:01
	 */
	@Action(value = "group_list_view")
	public String groupListView(){
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getDeptService().findGroupByEid(map, group, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		HttpServletResponse response = ServletActionContext.getResponse();
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 实例组信息添加界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:52:23
	 */
	@Action(value = "medical_group_add", results = {@Result(name = SUCCESS, location = "medical_group_add.jsp")})
	public String groupAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 平台信息列表
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,下午2:48:24
	 */
	@Action(value = "group_platfrom_view")
	public String platformListView(){
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getDeptService().findPlatformList(admin.getEnterpriseId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 保存组信息
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午10:29:51
	 */
	@Action(value = "medical_group_save")
	public String groupSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		String platformId = request.getParameter("platformId");
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getDeptService().saveGroup(group, admin, platformId);
		if(flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 打开组信息修改界面
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午9:52:23
	 */
	@Action(value = "medical_group_edit", results = {@Result(name = SUCCESS, location = "medical_group_edit.jsp")})
	public String groupEdit(){
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑的组信息 
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月14日,下午10:30:54
	 */
	@Action(value = "medical_groupedit_save")
	public String groupEditSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String platformId = request.getParameter("platformId");
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(group.getId())){
			boolean flag = this.baseAllService.getDeptService().saveEditGroup(group, platformId);
			if(flag){
				map.put("success", "true");
			}else{
				map.put("success", "false");
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 验证同一个中心下是否已存在当前添加的组
	 * @return null
	 * @author dangjh
	 * 2014年12月24日 下午4:56:40
	 */
	@Action(value = "center_include_group")
	public String includeGroup() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String platformId = request.getParameter("platformId");
		map = new HashMap<String, Object>();
		boolean flag = this.baseAllService.getDeptService().includeGroup(platformId, group.getName());
		if(!flag){
			map.put("success", true);
		}else{
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 组下是否包含用户
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,上午11:34:10
	 */
	@Action(value = "group_include_user")
	public String includeUser(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(group.getId())){
			boolean flag = this.baseAllService.getDeptService().includeGroupUser(group.getId());
			if(flag){
				map.put("success", "true");
			}
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 删除组
	 * @return
	 * @author dangjh
	 * @datetime 2014年10月15日,上午11:33:08
	 */
	@Action(value = "meidcal_group_del")
	public String groupDel(){
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(group.getId())){
			this.baseAllService.getDeptService().delGroup(group);
			map.put("success", "true");
		}else{
			map.put("success", "error");
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalGroup getModel() {
		return group;
	}

}
